package com.geek.bean.data;

import com.geek.model.MarketCart;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author 小豪
 * @version 1.0
 * @description 购物车商品状态判断
 * @date 2024-06-13 9:20
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class CartStat {

    private Integer goodsCount;
    private Integer checkedGoodsCount;
    private BigDecimal goodsAmount;
    private BigDecimal checkedGoodsAmount;

    public static CartStat stat(List<MarketCart> cartList) {
        // cartList中的所有商品
        int goodsCount = cartList.stream().mapToInt(MarketCart::getNumber).sum();
        BigDecimal goodsAmount = cartList.stream().map(item -> {
            BigDecimal price = item.getPrice();
            int number = item.getNumber();
            return price.multiply(BigDecimal.valueOf(number));
        }).reduce(BigDecimal.ZERO, BigDecimal::add);

        // 挑选出check = true的
        int checkedGoodsCount = cartList.stream().filter(MarketCart::getChecked).mapToInt(MarketCart::getNumber).sum();
        BigDecimal checkedGoodsAmount = cartList.stream().filter(MarketCart::getChecked).map(item -> {
            BigDecimal price = item.getPrice();
            int number = item.getNumber();
            return price.multiply(BigDecimal.valueOf(number));
        }).reduce(BigDecimal.ZERO, BigDecimal::add);
        CartStat stat = new CartStat();
        stat.setGoodsCount(goodsCount);
        stat.setGoodsAmount(goodsAmount);
        stat.setCheckedGoodsAmount(checkedGoodsAmount);
        stat.setCheckedGoodsCount(checkedGoodsCount);
        return stat;
    }

}
